<?php
class SubscriptionsController extends Controller
{
	public $layout = 'main';
	
	public function actionIndex()
	{
		$this->forward(LINK_ADMINCP_SUBSCRIPTIONS.'/subscribers');
	}
	
	public function actionSubscribers()
	{
		//get params
		$intPage 		= Yii::app()->request->getParam('page', 1);
		$intRowsPerPage = Yii::app()->request->getParam('rpp', MIN_ROWS_PER_PAGE);
		$intSortType 	= Yii::app()->request->getParam('sort',-1);
		
		//list subscribers
		$args = array(
			'keyword' => null
		);
		$arrSubscribers = Subscriptions::getInstance()->getListSubscribers($args, $intPage, $intRowsPerPage);
		$intTotalRows = isset($arrUsers[0]['total_rows'])?$arrUsers[0]['total_rows']:0;
		
		//pagination
		$objPages=new CPagination($intTotalRows);
		$objPages->setCurrentPage($intPage-1);
		$objPages->setPageSize($intRowsPerPage);
		
		//render view
		$this->render('subscribers', array(
			'arrSubscribers' => $arrSubscribers,
			'objPages' => $objPages,
			'args' => $args
		));
	}
	
	public function actionEditSubscriber()
	{
		$intId = Yii::app()->request->getParam('id', null);
		//update
		if(Yii::app()->request->isPostRequest)
		{
			//update subscriber info
			$arrParams = Yii::app()->request->getParam('subscriber', null);
			if(empty($intId)){
				$intResult = Subscriptions::getInstance()->insertSubscriber($arrParams);
				$intId = $intResult;
			}
			else{
				$intResult = Subscriptions::getInstance()->updateSubscriber($intId, $arrParams);
			}
			//update mailing list
			$arrParams = Yii::app()->request->getParam('list', null); //o($arrParams);
			Subscriptions::getInstance()->updateSubscriberInList($intId, $arrParams);
			
			Yii::app()->admin->setFlash('ok', Yii::t('admincp', 'Updated successfully!'));
			$this->refresh();
		}
		//load form
		$arrItemInfo = array();
		if(!empty($intId)){
			$arrItemInfo = Subscriptions::getInstance()->getSubscriber($intId);
		}
		$arrLists = Subscriptions::getInstance()->getListMailingLists(array(), 1, 100);
		//render layout
		$this->render('subscribers_form', array(
			'arrItemInfo' => $arrItemInfo,
			'arrLists' => $arrLists
		));
	}
	
	public function actionDeleteSubscriber(){
		$intId = Yii::app()->request->getParam('id', null);
		if(!empty($intId))
			$result = Subscriptions::getInstance()->deleteSubscriber($intId);
		header('Location: '.LINK_ADMINCP_SUBSCRIPTIONS);
	}
	
	public function actionLists(){
		//get params
		$intPage 		= Yii::app()->request->getParam('page', 1);
		$intRowsPerPage = Yii::app()->request->getParam('rpp', MIN_ROWS_PER_PAGE);
		$intSortType 	= Yii::app()->request->getParam('sort',-1);
		
		//get mailing list
		$args = array(
			'keyword' => null
		);
		$arrLists = Subscriptions::getInstance()->getListMailingLists($args, $intPage, $intRowsPerPage);
		$intTotalRows = isset($arrLists[0]['total_rows'])?$arrLists[0]['total_rows']:0;
		
		//pagination
		$objPages=new CPagination($intTotalRows);
		$objPages->setCurrentPage($intPage-1);
		$objPages->setPageSize($intRowsPerPage);
		
		//render view
		$this->render('lists', array(
			'arrLists' => $arrLists,
			'objPages' => $objPages,
			'args' => $args
		));
	}
	
	public function actionEditList()
	{
		$intId = Yii::app()->request->getParam('id', null);
		//update
		if(Yii::app()->request->isPostRequest)
		{
			$arrParams = Yii::app()->request->getParam('list', null);
			if(empty($intId)){
				$intResult = Subscriptions::getInstance()->insertMailingList($arrParams);
				$intId = $intResult;
			}
			else{
				$intResult = Subscriptions::getInstance()->updateMailingList($intId, $arrParams);
			}
			Yii::app()->admin->setFlash('ok', Yii::t('admincp', 'Updated successfully!'));
			$this->refresh();
		}
		//load form
		$arrItemInfo = array();
		if(!empty($intId)){
			$arrItemInfo = Subscriptions::getInstance()->getMailingList($intId);
		}

		//render layout
		$this->render('list_form', array(
			'arrItemInfo' => $arrItemInfo
		));
	}
	
	public function actionDeleteList(){
		$intId = Yii::app()->request->getParam('id', null);
		if(!empty($intId))
			$result = Subscriptions::getInstance()->deleteMailingList($intId);
		header('Location: '.LINK_ADMINCP_SUBSCRIPTIONS.'/lists');
	}
	
	public function actionEmails(){
		//get params
		$intPage 		= Yii::app()->request->getParam('page', 1);
		$intRowsPerPage = Yii::app()->request->getParam('rpp', MIN_ROWS_PER_PAGE);
		$intSortType 	= Yii::app()->request->getParam('sort',-1);
		
		//get mailing list
		$args = array(
			'keyword' => null
		);
		$arrEmails = Subscriptions::getInstance()->getListEmails($args, $intPage, $intRowsPerPage);
		$intTotalRows = isset($arrEmails[0]['total_rows'])?$arrEmails[0]['total_rows']:0;
		
		//pagination
		$objPages=new CPagination($intTotalRows);
		$objPages->setCurrentPage($intPage-1);
		$objPages->setPageSize($intRowsPerPage);
		
		//render view
		$this->render('emails', array(
			'arrEmails' => $arrEmails,
			'objPages' => $objPages,
			'args' => $args
		));
	}
	
	public function actionEditEmail()
	{
		$intId = Yii::app()->request->getParam('id', null);
		//update
		if(Yii::app()->request->isPostRequest)
		{
			//update subscriber info
			$arrParams = Yii::app()->request->getParam('mail', null);
			if(empty($intId)){
				$intResult = Subscriptions::getInstance()->insertEmail($arrParams);
				$intId = $intResult;
			}
			else{
				$intResult = Subscriptions::getInstance()->updateEmail($intId, $arrParams);
			}
			//update mailing list
			$arrParams = Yii::app()->request->getParam('list', null);
			$args = array();
			foreach($arrParams as $key=>$value){
				if($value == '1'){
					$args[] = $key;
				}
			}
			Subscriptions::getInstance()->updateEmailInList($intId, $args);
			
			Yii::app()->admin->setFlash('ok', Yii::t('admincp', 'Updated successfully!'));
			$this->refresh();
		}
		//load form
		$arrItemInfo = array();
		if(!empty($intId)){
			$arrItemInfo = Subscriptions::getInstance()->getEmail($intId);
		}
		$arrLists = Subscriptions::getInstance()->getListMailingLists(array(), 1, 100);
		//render layout
		$this->render('email_form', array(
			'arrItemInfo' => $arrItemInfo,
			'arrLists' => $arrLists
		));
	}
	
	public function actionDeleteEmail(){
		$intId = Yii::app()->request->getParam('id', null);
		if(!empty($intId))
			$result = Subscriptions::getInstance()->deleteEmail($intId);
		header('Location: '.LINK_ADMINCP_SUBSCRIPTIONS.'/emails');
	}
	
	public function actionPreview(){
		$intId = Yii::app()->request->getParam('id', null);
		$arrItemInfo = Subscriptions::getInstance()->getEmail($intId);
		$this->renderPartial('preview', array(
			'arrItemInfo' => $arrItemInfo
		));
	}
	
	public function actionSend(){
		$intEmailId = Yii::app()->request->getParam('id', null);
		//do send
		if(Yii::app()->request->isPostRequest)
		{
			
		}
		//load data for sending
		$arrItemInfo = Subscriptions::getInstance()->getEmail($intId);
		if(empty($arrItemInfo)){
			echo 'invalid email';	
		}
		//render layout
		$this->render('send', array(
			'arrItemInfo' => $arrItemInfo
		));
	}
}